package com.ocom.aiot.controller;

import com.ocom.aiot.service.XfDeviceInfoService;
import com.ocom.common.constant.AiotUrl;
import com.ocom.common.entity.aiot.EntityXfDeviceInfo;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.DateUtil;
import com.ocom.common.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: TODO
 * @Author: 朱晖
 * @Date: 2023/7/13
 **/
@Slf4j
@RestController
public class DeShengDeviceController {



	@Autowired
	private XfDeviceInfoService xfDeviceInfoService;


	@PostMapping(AiotUrl.Url_Aiot_DeShengAddMach)
	public Result addMach(Long comId,String deviceNo){
		try {
			EntityXfDeviceInfo entityXfDeviceInfo = xfDeviceInfoService.GetDeviceByDeviceNo(deviceNo);

			if (entityXfDeviceInfo != null){
				if(entityXfDeviceInfo.getStatus()==1 && !entityXfDeviceInfo.getComId().equals(comId)){
					return ResultUtil.info(203,"设备已激活");
				}

				entityXfDeviceInfo.setEndTime(DateUtil.strToDateLong("2123-01-01 13:48:51"));
				entityXfDeviceInfo.setStatus(1l);
				entityXfDeviceInfo.setComId(comId);
				entityXfDeviceInfo.setPComId(comId);
				entityXfDeviceInfo.setSaasNo("1");
				entityXfDeviceInfo.setCommMode(1l);
				entityXfDeviceInfo.setAgentId(1l);
				entityXfDeviceInfo.setDeviceType(1l);
				entityXfDeviceInfo.setDeviceVerType(1l);

				//改参数
				xfDeviceInfoService.UpdateDeviceInfo(entityXfDeviceInfo);

			} else {
				try {
					EntityXfDeviceInfo entityXfDeviceInfo2 = new EntityXfDeviceInfo();
					entityXfDeviceInfo2.setComId(comId);
					entityXfDeviceInfo2.setPComId(comId);
					entityXfDeviceInfo2.setDeviceNo(deviceNo);
					entityXfDeviceInfo2.setPSaasNo("1");
					entityXfDeviceInfo2.setCommMode(1l);
					entityXfDeviceInfo2.setDeviceType(1l);
					entityXfDeviceInfo2.setDeviceVerType(1l);
					entityXfDeviceInfo2.setEndTime(DateUtil.strToDateLong("2123-01-01 13:48:51"));
					entityXfDeviceInfo2.setPAgentId(1l);
					entityXfDeviceInfo2.setAgentId(1l);
					entityXfDeviceInfo2.setSaasNo("1");
					entityXfDeviceInfo2.setStatus(1l);
					xfDeviceInfoService.addDeviceInfo(entityXfDeviceInfo2);
				}catch (Exception exception){
					log.error(exception.getMessage());
					return ResultUtil.error(ResultEnum.Err_2009,exception.getMessage());
				}
			}
		}catch (Exception exception){
			log.error("添加设备异常  【{}】 ",exception  );
		}
		return ResultUtil.success();
	}
}
